package com.ruoyi.service.impl;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.ruoyi.domain.UserInfo;
import com.ruoyi.mapper.UserInfoMapper;
import com.ruoyi.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2023-01-13 17:55:17
 */
@Service
@Slf4j
public class UserInfoServiceImpl implements IUserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void login(WxMaJscode2SessionResult result, String inviteId) {
        UserInfo userInfo = new UserInfo();
        userInfo.setOpenId(result.getOpenid());
        userInfo.setUnionId(result.getUnionid());

        Weekend<UserInfo> weekend = new Weekend<>(UserInfo.class);
        WeekendCriteria<UserInfo, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andEqualTo(UserInfo::getOpenId, result.getOpenid());

        UserInfo data = userInfoMapper.selectOneByExample(weekend);
        log.info("获取用户登录信息[{}][{}]");
        if (null == data) {
            userInfo.setInviteId(inviteId);
            userInfoMapper.insertSelective(userInfo);
        }
    }

    @Override
    public void upsertUserInfo(String openId, WxMaUserInfo userInfo) {
        Weekend<UserInfo> weekend = new Weekend<>(UserInfo.class);
        WeekendCriteria<UserInfo, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andEqualTo(UserInfo::getOpenId, openId);

        UserInfo data = userInfoMapper.selectOneByExample(weekend);
        if (null == data) {
            UserInfo info = new UserInfo();
            info.setOpenId(openId);
            info.setNickName(userInfo.getNickName());
            info.setGender(userInfo.getGender());
            info.setLanguage(userInfo.getLanguage());
            info.setCity(userInfo.getCity());
            info.setProvince(userInfo.getProvince());
            info.setCountry(userInfo.getCountry());
            userInfoMapper.insertSelective(info);
        } else {
            data.setNickName(userInfo.getNickName());
            data.setGender(userInfo.getGender());
            data.setLanguage(userInfo.getLanguage());
            data.setCity(userInfo.getCity());
            data.setProvince(userInfo.getProvince());
            data.setCountry(userInfo.getCountry());
            userInfoMapper.updateByPrimaryKeySelective(data);
        }
    }

    @Override
    public void upsertPhoneNo(String openId, WxMaPhoneNumberInfo phoneNumberInfo) {
        Weekend<UserInfo> weekend = new Weekend<>(UserInfo.class);
        WeekendCriteria<UserInfo, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andEqualTo(UserInfo::getOpenId, openId);

        UserInfo data = userInfoMapper.selectOneByExample(weekend);
        if (null == data) {
            UserInfo info = new UserInfo();
            info.setOpenId(openId);
            info.setPhone(phoneNumberInfo.getPhoneNumber());
            userInfoMapper.insertSelective(info);
        } else {
            data.setPhone(phoneNumberInfo.getPhoneNumber());
            userInfoMapper.updateByPrimaryKeySelective(data);
        }
    }
}
